﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using CommonLibrary.Data;

namespace CommonLibrary
{
    public class RequestHelper
    {
        /// <summary>
        /// 获取string类型的参数
        /// </summary>
        /// <param name="key"></param>
        /// <returns>参数为空返回""</returns>
        public static string GetString(string key)
        {
            //包含以上两种方式(优先获取GET方式提交的数据)，它会在QueryString、Form、ServerVariable中都按先后顺序搜寻一遍。

            string keyValue = HttpContext.Current.Request[key];
            return keyValue == null ? string.Empty : keyValue;
        }

        /// <summary>
        /// 获取Bool类型的参数
        /// </summary>
        /// <param name="key"></param>
        /// <returns>参数为空或转换出错返回false</returns>
        public static bool GetBool(string key)
        {
            string keyValue = GetString(key);
            return Convert.ToBoolean(keyValue);
        }

        /// <summary>
        /// 获取时间类型的参数
        /// </summary>
        /// <param name="key"></param>
        /// <returns>参数为空或转换出错返回1900-1-1</returns>
        public static DateTime GetDateTime(string key)
        {
            string keyValue = GetString(key);
            DateTime dt = new DateTime(1900, 1, 1);
            try
            {
                dt = Convert.ToDateTime(keyValue);
            }
            catch
            { }
            return dt;
        }

        /// <summary>
        /// 获取时间类型的参数20130620103505
        /// </summary>
        /// <param name="key"></param>
        /// <returns>参数为空或转换出错返回1900-1-1</returns>
        public static DateTime GetDateTimeNum(string key)
        {
            string keyValue = GetString(key);
            return keyValue.ClToDateTimeNum();
        }

        /// <summary>
        /// 获取Int32类型的参数
        /// </summary>
        /// <param name="key"></param>
        /// <returns>参数为空或转换出错返回0</returns>
        public static int GetInt(string key)
        {
            string keyValue = GetString(key);
            return Convert.ToInt32(keyValue);
        }

        /// <summary>
        /// 获取Double类型的参数
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static double GetDouble(string key)
        {
            string keyValue = GetString(key);
            return Convert.ToDouble(keyValue);
        }



        /// <summary>
        /// 将QueryString转换为Dictionary
        /// </summary>
        /// <returns>request回来的信息组成的数组</returns>
        public static Dictionary<string, string> RequestQueryStringToDictionary()
        {
            //Request.QueryString会默认做一次HttpUtility.UrlEncode，所有HttpUtility.UrlEncode不要与HttpUtility.UrlDecode对应

            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            foreach (string key in HttpContext.Current.Request.QueryString.AllKeys)
            {
                dictionary.Add(key, HttpContext.Current.Request.QueryString[key]);
            }
            return dictionary;
        }

        /// <summary>
        /// 将Form转换为Dictionary
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> RequestFormToDictionary()
        {
            //Request.QueryString会默认做一次HttpUtility.UrlEncode，所有HttpUtility.UrlEncode不要与HttpUtility.UrlDecode对应

            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            foreach (string key in HttpContext.Current.Request.Form.AllKeys)
            {
                dictionary.Add(key, HttpContext.Current.Request.Form[key]);
            }
            return dictionary;
        }

        /// <summary>
        /// Get、Post参数转化为Dictionary
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> RequestQueryStringAndFormToDictionary()
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            foreach (string key in HttpContext.Current.Request.QueryString.AllKeys)
            {
                if (dictionary.Keys.Where(a => a.ToUpper() == key.ToUpper()).ToList().Count > 0)
                {
                    continue;
                }
                dictionary.Add(key, HttpContext.Current.Request.QueryString[key]);
            }
            foreach (string key in HttpContext.Current.Request.Form.AllKeys)
            {
                if (dictionary.Keys.Where(a => a.ToUpper() == key.ToUpper()).ToList().Count > 0)
                {
                    continue;
                }
                dictionary.Add(key, HttpContext.Current.Request.Form[key]);
            }
            return dictionary;
        }


        /// <summary>
        /// 将QueryString转换为对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T RequestQueryStringToObject<T>()
        {
            Dictionary<string, string> dictionary = RequestQueryStringToDictionary();
            return dictionary.DictionaryToObject<T>();
        }


        /// <summary>
        /// 将Form转换为对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T RequestFormToObject<T>()
        {
            Dictionary<string, string> dictionary = RequestFormToDictionary();
            return dictionary.DictionaryToObject<T>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T RequestQueryStringAndFormToObject<T>()
        {
            Dictionary<string, string> dictionary = RequestQueryStringAndFormToDictionary();
            return dictionary.DictionaryToObject<T>();
        }

        /// <summary>
        /// 获取当前Referrer "http://localhost:7759/MiugoApiIp/"
        /// </summary>
        /// <returns></returns>
        public static string GetUrlReferrer()
        {
            if (HttpContext.Current.Request.UrlReferrer == null)
            {
                return "";
            }

            return HttpContext.Current.Request.UrlReferrer.OriginalString;
        }

    }
}
